import java.util.ArrayList;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author matheus
 */
public class Leitor_tipo_nomeJogador extends javax.swing.JFrame {

    /**
     * Creates new form Leitor_tipo_nomeJogador
     */
	
	private Integer tipo; //Identifica como cliente ou como servidor (1->Servidor, 0->Cliente)
	private ArrayList<String> nome; //Lista de Strings que guarda sempre um nome entrado pelo usuario
	private String ip; //Ip a se conectar, caso cliente
	
	//Getters dos campos
	public Integer getTipo() {
		return tipo;
	}

	public ArrayList<String> getNome() {
		return nome;
	}
	
	public String getIp() {
		return ip;
	
	}
    public Leitor_tipo_nomeJogador(ArrayList<String> nome) {
        initComponents();
        this.nome = nome;
        textIp.setEnabled(false);
        buttonServidor.setSelected(true);
        tipo = 1;
    }

 
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        buttonGroup1 = new javax.swing.ButtonGroup();
        buttonServidor = new javax.swing.JRadioButton();
        buttonCliente = new javax.swing.JRadioButton();
        jLabel1 = new javax.swing.JLabel();
        textNome = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        buttonDone = new javax.swing.JButton();
        textIp = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        buttonGroup1.add(buttonServidor);
        buttonServidor.setText("Servidor");
        buttonServidor.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonServidorActionPerformed(evt);
            }
        });

        buttonGroup1.add(buttonCliente);
        buttonCliente.setText("Cliente");
        buttonCliente.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonClienteActionPerformed(evt);
            }
        });

        jLabel1.setText("Servidor ou cliente?");

        textNome.setText("Nome");
        textNome.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                textNomeActionPerformed(evt);
            }
        });

        jLabel3.setText("Seu nome?");

        buttonDone.setText("Pronto");
        buttonDone.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonDoneActionPerformed(evt);
            }
        });

        textIp.setText("Ip");
        textIp.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                textIpActionPerformed(evt);
            }
        });

        jLabel4.setText("Ip a se conectar");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                        .addGroup(layout.createSequentialGroup()
                            .addGap(111, 111, 111)
                            .addComponent(jLabel1)
                            .addGap(85, 85, 85))
                        .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                            .addGap(31, 31, 31)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(buttonDone, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addGroup(layout.createSequentialGroup()
                                    .addComponent(jLabel4)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                    .addComponent(textIp))
                                .addComponent(textNome)))
                        .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                            .addGap(40, 40, 40)
                            .addComponent(buttonServidor, javax.swing.GroupLayout.PREFERRED_SIZE, 109, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(buttonCliente)
                            .addGap(50, 50, 50)))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(138, 138, 138)
                        .addComponent(jLabel3)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(buttonServidor)
                    .addComponent(buttonCliente))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(textIp, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel4))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel3)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(textNome, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(buttonDone)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void buttonServidorActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonServidorActionPerformed
        textIp.setEnabled(false);
        tipo = 1;
    }

    private void buttonClienteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonClienteActionPerformed
    	textIp.setEnabled(true); //Habilita para digitar o Ip
    	tipo  = 0;
    }

    private void textIpActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_textIpActionPerformed
    }//GEN-LAST:event_textIpActionPerformed

    private void textNomeActionPerformed(java.awt.event.ActionEvent evt) {
    }

    private void buttonDoneActionPerformed(java.awt.event.ActionEvent evt) {
    	synchronized (nome) {
    		if(tipo != -1){
    			this.setVisible(false);
    			this.nome.set(0, textNome.getText());
    			ip = textIp.getText();
    			nome.notify(); //Notifica que ja tem o nome e o Ip desejado
    		}
    	}
    }
    
 
    private javax.swing.JRadioButton buttonCliente;
    private javax.swing.JButton buttonDone;
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.JRadioButton buttonServidor;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JTextField textIp;
    private javax.swing.JTextField textNome;
}
